package ua.kharkov.khpi.tarasenko.bach.analysis.prefixspan;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Sequence implements Cloneable {
	private List<Item> sequence = new ArrayList<Item>();

	// There we will store items in our sequence for fast contains method
	private Set<Item> itemSet = new HashSet<Item>();

	@Override
	public String toString() {

		String result = "";
		for (Item item : sequence) {
			result = result + item + " ";
		}
		return result;
	}

	public boolean isEmpty() {
		boolean result = true;
		if (sequence.size() > 0) {
			result = false;
		}

		return result;
	}

	public void addItem(Item item) {
		sequence.add(item);
		itemSet.add(item);
	}

	public void addSequence(Sequence anSequence) {
		sequence.addAll(anSequence.sequence);
		itemSet.addAll(anSequence.sequence);
	}

	public List<Item> getItems() {
		return sequence;
	}

	public String[] getItemsIds() {
		String[] ids = new String[itemSet.size()];
		
		int count = 0;
		for (Item item : itemSet) {
			ids[count] = item.getItem();
			count++;
		}
		return ids;
	}
	
	public Sequence subsequence(int startIndex) {
		Sequence subSeq = new Sequence();
		List<Item> newSeq = Collections.EMPTY_LIST;

		if (!(startIndex >= sequence.size())) {
			newSeq = new ArrayList<Item>(sequence.subList(startIndex,
					sequence.size()));
		}
		subSeq.sequence = newSeq;
		return subSeq;
	}

	public int indexOf(Item item) {
		int index = sequence.indexOf(item);
		return index;
	}
	
	public boolean contains(Item item) {
		return itemSet.contains(item);
	}
	
	public int size() {
		return sequence.size();
	}
	
	public void sortItems() {
		Collections.sort(sequence, new Comparator<Item>() {

			@Override
			public int compare(Item o1, Item o2) {
				return Integer.valueOf(o1.getItem()).compareTo(Integer.valueOf(o2.getItem()));
			}

		});
	}
}
